﻿using AutoMapper;
using HamcoWcs.Entitys.BaseConfigs;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using HamcoWcs.Models.Monitors;
using HamcoWcs.Models.NodeInfos;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HamcoWcs.Service.Common
{
    public partial class AutomaticStorageInfo
    {
        private readonly IServiceScopeFactory _scopeFactory;
        private readonly IMapper _mapper;
        /// <summary>
        /// 全局监控信息：BCR，安全光栅，提升机、RGV的Teaching数据等信息
        /// </summary>
        public GlobalMonitor GlobalMonitor { get; set; } = new GlobalMonitor();

        /// <summary>
        /// 异常（缓存）
        /// </summary>
        public static List<AlarmExplanation> Alarms { get; private set; }

        /// <summary>
        /// 堆垛机集合
        /// </summary>
        public IEnumerable<SrmDto> Srms
        {
            get
            {
                var list = new List<SrmDto>() { };
                foreach (var node in Nodes.Where(x => x.DeviceType == EnumNodeType.srm))
                {
                    list.AddRange(node.Srms);
                }
                return list;
            }
        }

        /// <summary>
        /// CV，Lift，RGV集合
        /// </summary>
        public IEnumerable<PconDto> Pcons
        {
            get
            {
                var list = new List<PconDto>() { };
                foreach (var node in Nodes.Where(x => x.DeviceType != EnumNodeType.srm))
                {
                    list.AddRange(node.Pcons);
                }
                return list;
            }
        }

        /// <summary>
        /// Hamco 主控PLC
        /// </summary>
        public Dictionary<string, PconPlc> ConveyorDic { get; set; } = new Dictionary<string, PconPlc>();

        /// <summary>
        /// 堆垛机 地上盘PLC
        /// </summary>
        public Dictionary<string, SrmPlc> SrmDic { get; set; } = new Dictionary<string, SrmPlc>();

        /// <summary>
        /// 节点
        /// </summary>
        public List<NodeInfoDto> Nodes { get; set; }


        public List<MotorMileage> MotorMileages { get; set; } = new List<MotorMileage>
        {
            new MotorMileage(902 ,"P0001输送电机里程", "CJRCV100","01"),
            new MotorMileage(904 ,"P0001挡板电机里程", "CJRCV100","01"),
            new MotorMileage(906 ,"P0001对中电机里程", "CJRCV100","01"),
            new MotorMileage(908 ,"P0002输送电机里程", "CJRCV100","02"),
            new MotorMileage(910 ,"P0003输送电机里程", "CJRCV200","01"),
            new MotorMileage(912 ,"P0003挡板电机里程", "CJRCV200","01"),
            new MotorMileage(914 ,"P0003对中电机里程", "CJRCV200","01"),
            new MotorMileage(916 ,"P0004输送电机里程", "CJRCV200","02"),
            new MotorMileage(918 ,"P0005输送电机里程", "CJRCV300","01"),
            new MotorMileage(920 ,"P0005挡板电机里程", "CJRCV300","01"),
            new MotorMileage(922 ,"P0005对中电机里程", "CJRCV300","01"),
            new MotorMileage(924 ,"P0006输送电机里程", "CJRCV300","02"),
            new MotorMileage(926 ,"P0007输送电机里程", "CJRCV400","01"),
            new MotorMileage(928 ,"P0007挡板电机里程", "CJRCV400","01"),
            new MotorMileage(930 ,"P0007对中电机里程", "CJRCV400","01"),
            new MotorMileage(932 ,"P0008输送电机里程", "CJRCV400","02"),
            new MotorMileage(934 ,"P0009输送电机里程", "CJVCV100","01"),
            new MotorMileage(936 ,"P0010输送电机里程", "CJVCV500","06"),
            new MotorMileage(938 ,"P0011输送电机里程", "CJVCV500","05"),
            new MotorMileage(940 ,"P0012输送电机里程", "CJVCV500","04"),
            new MotorMileage(942 ,"P0013输送电机里程", "CJVCV500","03"),
            new MotorMileage(944 ,"P0014输送电机里程", "CJVCV500","02"),
            new MotorMileage(946 ,"P0015输送电机里程", "CJVCV500","01"),
            new MotorMileage(948 ,"P0016输送电机里程", "CJVCV600","06"),
            new MotorMileage(950 ,"P0017输送电机里程", "CJVCV600","05"),
            new MotorMileage(952 ,"P0018输送电机里程", "CJVCV600","04"),
            new MotorMileage(954 ,"P0019输送电机里程", "CJVCV600","03"),
            new MotorMileage(956 ,"P0020输送电机里程", "CJVCV600","02"),
            new MotorMileage(958 ,"P0021输送电机里程", "CJVCV600","01"),
            new MotorMileage(960 ,"P0022输送电机里程", "CJVCV700","07"),
            new MotorMileage(962 ,"P0023输送电机里程", "CJVCV700","06"),
            new MotorMileage(964 ,"P0024输送电机里程", "CJVCV700","05"),
            new MotorMileage(966 ,"P0025输送电机里程", "CJVCV700","04"),
            new MotorMileage(968 ,"P0026输送电机里程", "CJVCV700","03"),
            new MotorMileage(970 ,"P0027输送电机里程", "CJVCV700","02"),
            new MotorMileage(972 ,"P0027对中电机里程", "CJVCV700","02"),
            new MotorMileage(974 ,"P0028输送电机里程", "CJVCV700","01"),
            new MotorMileage(976 ,"P0029输送电机里程", "CJVCV800","07"),
            new MotorMileage(978 ,"P0030输送电机里程", "CJVCV800","06"),
            new MotorMileage(980 ,"P0031输送电机里程", "CJVCV800","05"),
            new MotorMileage(982 ,"P0032输送电机里程", "CJVCV800","04"),
            new MotorMileage(984 ,"P0033输送电机里程", "CJVCV800","03"),
            new MotorMileage(986 ,"P0034输送电机里程", "CJVCV800","02"),
            new MotorMileage(988 ,"P0034对中电机里程", "CJVCV800","02"),
            new MotorMileage(990 ,"P0035输送电机里程", "CJVCV800","01"),
            new MotorMileage(992 ,"P0036输送电机里程", "CJVCV200","01"),
            new MotorMileage(994 ,"P0037输送电机里程", "CJMIFC00","01"),
            new MotorMileage(996 ,"P0037升降电机里程", "CJMIFC00","01"),
            new MotorMileage(998 ,"P0038输送电机里程", "CJMIFB00","01"),
            new MotorMileage(1000 ,"P0038升降电机里程", "CJMIFB00","01"),
            new MotorMileage(1002 ,"P0039输送电机里程", "CJVCV300","02"),
            new MotorMileage(1004 ,"P0039挡板电机里程", "CJVCV300","02"),
            new MotorMileage(1006 ,"P0040输送电机里程", "CJVCV300","01"),
            new MotorMileage(1008 ,"P0040对中电机里程", "CJVCV300","01"),
            new MotorMileage(1010 ,"P0041输送电机里程", "CJVCV400","02"),
            new MotorMileage(1012 ,"P0041挡板电机里程", "CJVCV400","02"),
            new MotorMileage(1014 ,"P0042输送电机里程", "CJVCV400","01"),
            new MotorMileage(1016 ,"P0042对中电机里程", "CJVCV400","01"),
            // L20
            new MotorMileage(1018 ,"P1002输送电机里程", "CJMIF900","01"),
            new MotorMileage(1020 ,"P1002挡板电机里程", "CJMIF900","01"),
            new MotorMileage(1022 ,"P1002升降电机里程", "CJMIF900","01"),
            new MotorMileage(1024 ,"P1003输送电机里程", "CJMCV900","01"),
            new MotorMileage(1026 ,"P1005输送电机里程", "CJMIF800","01"),
            new MotorMileage(1028 ,"P1005挡板电机里程", "CJMIF800","01"),
            new MotorMileage(1030 ,"P1005升降电机里程", "CJMIF800","01"),
            new MotorMileage(1032 ,"P1006输送电机里程", "CJMCV800","01"),
            new MotorMileage(1034 ,"P1008输送电机里程", "CJMIF700","01"),
            new MotorMileage(1036 ,"P1008挡板电机里程", "CJMIF700","01"),
            new MotorMileage(1038 ,"P1008升降电机里程", "CJMIF700","01"),
            new MotorMileage(1040 ,"P1009输送电机里程", "CJMCV700","01"),
            new MotorMileage(1042 ,"P1010输送电机里程", "CJVCVB00","01"),
            new MotorMileage(1044 ,"P1010挡板电机里程", "CJVCVB00","01"),
            new MotorMileage(1046 ,"P1012输送电机里程", "CJBUF104","01"),
            new MotorMileage(1048 ,"P1013输送电机里程", "CJVCVC00","01"),
            new MotorMileage(1050 ,"P1013挡板电机里程", "CJVCVC00","01"),
            new MotorMileage(1052 ,"P1015输送电机里程", "CJBUF103","01"),
            new MotorMileage(1054 ,"P1016输送电机里程", "CJBUF102","01"),
            new MotorMileage(1056 ,"P1017输送电机里程", "CJBUF101","01"),
            new MotorMileage(1058 ,"P1019输送电机里程", "CJMIF600","01"),
            new MotorMileage(1060 ,"P1019挡板电机里程", "CJMIF600","01"),
            new MotorMileage(1062 ,"P1019升降电机里程", "CJMIF600","01"),
            new MotorMileage(1064 ,"P1020输送电机里程", "CJMCV600","03"),
            new MotorMileage(1066 ,"P1021输送电机里程", "CJMCV600","02"),
            new MotorMileage(1068 ,"P1022输送电机里程", "CJMCV600","01"),
            new MotorMileage(1070 ,"P1023输送电机里程", "CJMCV500","03"),
            new MotorMileage(1072 ,"P1023挡板电机里程", "CJMCV500","03"),
            new MotorMileage(1074 ,"P1024输送电机里程", "CJMIF500","01"),
            new MotorMileage(1076 ,"P1024挡板电机里程", "CJMIF500","01"),
            new MotorMileage(1078 ,"P1024升降电机里程", "CJMIF500","01"),
            new MotorMileage(1080 ,"P1025输送电机里程", "CJMCV500","02"),
            new MotorMileage(1082 ,"P1026输送电机里程", "CJMCV500","01"),
            new MotorMileage(1084 ,"P1027输送电机里程", "CJMIF400","01"),
            new MotorMileage(1086 ,"P1027挡板电机里程", "CJMIF400","01"),
            new MotorMileage(1088 ,"P1027升降电机里程", "CJMIF400","01"),
            new MotorMileage(1090 ,"P1028输送电机里程", "CJMCV400","03"),
            new MotorMileage(1092 ,"P1029输送电机里程", "CJMCV400","02"),
            new MotorMileage(1094 ,"P1030输送电机里程", "CJMCV400","01"),
            new MotorMileage(1096 ,"P1031输送电机里程", "CJMIF300","01"),
            new MotorMileage(1098 ,"P1031挡板电机里程", "CJMIF300","01"),
            new MotorMileage(1100 ,"P1031升降电机里程", "CJMIF300","01"),
            new MotorMileage(1102 ,"P1032输送电机里程", "CJMCV300","01"),
            new MotorMileage(1104 ,"P1034输送电机里程", "CJMIF200","01"),
            new MotorMileage(1106 ,"P1034挡板电机里程", "CJMIF200","01"),
            new MotorMileage(1108 ,"P1034升降电机里程", "CJMIF200","01"),
            new MotorMileage(1110 ,"P1035输送电机里程", "CJMCV200","01"),
            new MotorMileage(1112 ,"P1037输送电机里程", "CJMIF100","01"),
            new MotorMileage(1114 ,"P1037挡板电机里程", "CJMIF100","01"),
            new MotorMileage(1116 ,"P1037升降电机里程", "CJMIF100","01"),
            new MotorMileage(1118 ,"P1038输送电机里程", "CJMCV100","01"),
            new MotorMileage(1120 ,"L10rgv01输送电机里程", "CJRGV200","C1"),
            new MotorMileage(1122 ,"L10rgv01走行电机里程", "CJRGV200","C1"),
            new MotorMileage(1124 ,"L20rgv02输送电机里程", "CJRGV100","C1"),
            new MotorMileage(1126 ,"L20rgv02走行电机里程", "CJRGV100","C1"),
            new MotorMileage(1128 ,"L20rgv03输送电机里程", "CJRGV100","C2"),
            new MotorMileage(1130 ,"L20rgv03走行电机里程", "CJRGV100","C2"),
            new MotorMileage(1132 ,"P1001输送电机里程", "CJMIFA00","01"),
            new MotorMileage(1134 ,"P1001挡板电机里程", "CJMIFA00","01"),
            new MotorMileage(1136 ,"P1001升降电机里程", "CJMIFA00","01"),
            new MotorMileage(1138 ,"P1004输送电机里程", "CJMIFA00","01"),
            // 短巷道
            new MotorMileage(0,"C1行走里程", "CJSTW100","01","SrmPlc01C1"),
            new MotorMileage(4,"C1升降里程", "CJSTW100","01","SrmPlc01C1"),
            new MotorMileage(8,"C1伸缩里程", "CJSTW100","01","SrmPlc01C1"),
            new MotorMileage(0,"C2行走里程", "CJSTW100","01","SrmPlc01C2"),
            new MotorMileage(4,"C2升降里程", "CJSTW100","01","SrmPlc01C2"),
            new MotorMileage(8,"C2伸缩里程", "CJSTW100","01","SrmPlc01C2"),
            // 长巷道
            new MotorMileage(0,"C1行走里程", "CJSTW200","01","SrmPlc02C1"),
            new MotorMileage(4,"C1升降里程", "CJSTW200","01","SrmPlc02C1"),
            new MotorMileage(8,"C1伸缩里程", "CJSTW200","01","SrmPlc02C1"),
            new MotorMileage(0,"C2行走里程", "CJSTW200","01","SrmPlc02C2"),
            new MotorMileage(4,"C2升降里程", "CJSTW200","01","SrmPlc02C2"),
            new MotorMileage(8,"C2伸缩里程", "CJSTW200","01","SrmPlc02C2"),

        };

        /// <summary>
        /// 重置里程用
        /// </summary>
        public List<MotorMileage> MotorReset = new List<MotorMileage>();

    }




    #region [ASRSException]
    /// <summary>
    /// ASRS中出现的异常
    /// </summary>
    public class ASRSException : Exception
    {
        /// <summary>
        /// 错误码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        public ASRSException(string message) : this(0, message)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        public ASRSException(int code, string message) : base(message)
        {
            Code = code;
        }
    }

    #endregion
}
